package com.gwz.wxtime.core.controller;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gwz.wxtime.chessboard.entity.AddWxDTO;
import com.gwz.wxtime.chessboard.service.TuLingService;
import com.gwz.wxtime.core.entity.base.BaseDTO;
import com.gwz.wxtime.core.entity.wx.GroupReceiveMessage;
import com.gwz.wxtime.core.entity.wx.WxReceiveMessageV1;
import com.gwz.wxtime.core.service.BaseService;
import com.gwz.wxtime.core.util.IpUtil;
import com.gwz.wxtime.core.util.GPT2Tokenizer;
import com.gwz.wxtime.core.util.WxUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

@RequestMapping("/")
@RestController
@Slf4j
public class HandController {

    @Autowired
    private List<BaseService> baseServices;


    @Autowired
    private TuLingService tuLingService;


    @PostMapping("/msg")
    public void handEvent(@RequestBody WxReceiveMessageV1 dto) {

        BaseDTO<BaseDTO<GroupReceiveMessage>> baseDTO = new BaseDTO<>();
        BaseDTO<GroupReceiveMessage> baseDTO1 = new BaseDTO<>();


        GroupReceiveMessage groupReceiveMessage = new GroupReceiveMessage();
        groupReceiveMessage.setTimeStamp(dto.getTimestamp() + "");
        groupReceiveMessage.setMsgType(dto.getMsgtype());
        groupReceiveMessage.setMsgSource(dto.getIsphonemsg());
        groupReceiveMessage.setFromWxid(dto.getWxid());
        groupReceiveMessage.setFinalFromWxid(dto.getSender());

        groupReceiveMessage.setMsg(dto.getContent());
        // groupReceiveMessage.setMsgBase64();


        baseDTO1.setData(groupReceiveMessage);
        baseDTO.setData(baseDTO1);

        if (dto.getWxid().contains("@chatroom")) {
  /*          BaseDTO<BaseDTO<GroupReceiveMessage>> baseDTO = JSON.parseObject(JSON.toJSONString(dto), new TypeReference<BaseDTO<BaseDTO<GroupReceiveMessage>>>() {
            });*/
            if (dto.getMsgtype() != 1) return;

            for (BaseService baseService : baseServices) {
                try {


                    boolean b = baseService.handGroupReceivedMessage(baseDTO);
                    if (b) break;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        } else {
            if (dto.getMsgtype() != 1) return;
            for (BaseService baseService : baseServices) {
                try {
                    boolean b = baseService.handPrivateChatSingleReceivedMessage(baseDTO);
                    if (b) break;
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

        }
   /*     if (dto.getEvent() == 10011) {
            BaseDTO<BaseDTO<AddWxDTO>> baseDTO = JSON.parseObject(JSON.toJSONString(dto), new TypeReference<BaseDTO<BaseDTO<AddWxDTO>>>() {
            });
            addWx(baseDTO.getData().getData());
        }*/
    }


    @GetMapping("/sendMessage")
    public String sendMessage(HttpServletRequest request, @RequestParam String message) {
        String chatgptReplay = tuLingService.getChatgptReplay(IpUtil.getIpAddress(request), message);
        return chatgptReplay;
    }

    @SneakyThrows
    private void addWx(AddWxDTO addWxDTO) {
        System.out.println(addWxDTO);
        WxUtil.sendAgreeToFriendRequest(addWxDTO.getV3(), addWxDTO.getV4(), addWxDTO.getScene());
        Thread.sleep(500);
        WxUtil.sendMessage(addWxDTO.getWxid(), "您好" + addWxDTO.getNick() + " 有什么可以帮助到你的呢");
    }

}
